"""
字典表 Schema
"""

from datetime import datetime
from typing import Optional

from pydantic import Field, field_validator

from app.schemas.base import BaseSchema


class DictBase(BaseSchema):
    """字典表 基础模型"""

    dict_code: Optional[str] = Field(default=None, max_length=50, description="类型编码", alias="dictCode")
    name: Optional[str] = Field(default=None, max_length=50, description="类型名称", alias="name")
    status: Optional[int] = Field(default=None, description="状态(0:正常;1:禁用)", alias="status")
    remark: Optional[str] = Field(default=None, max_length=255, description="备注", alias="remark")


class DictCreate(DictBase):
    """创建字典表 模型"""

    pass


class DictUpdate(DictBase):
    """更新字典表 模型"""

    dict_code: Optional[str] = Field(default=None, max_length=50, description="类型编码", alias="dictCode")
    name: Optional[str] = Field(default=None, max_length=50, description="类型名称", alias="name")
    status: Optional[int] = Field(default=None, description="状态(0:正常;1:禁用)", alias="status")
    remark: Optional[str] = Field(default=None, max_length=255, description="备注", alias="remark")


class DictPublic(DictBase):
    """字典表 公共模型，用于API响应"""

    id: int = Field(alias="id", description="主键 ")
    dict_code: Optional[str] = Field(default=None, alias="dictCode", max_length=50, description="类型编码")
    name: Optional[str] = Field(default=None, alias="name", max_length=50, description="类型名称")
    status: Optional[int] = Field(default=None, alias="status", description="状态(0:正常;1:禁用)")
    remark: Optional[str] = Field(default=None, alias="remark", max_length=255, description="备注")
    create_time: Optional[datetime] = Field(default=None, alias="createTime", description="创建时间")
    create_by: Optional[int] = Field(default=None, alias="createBy", description="创建人ID")
    update_time: Optional[datetime] = Field(default=None, alias="updateTime", description="更新时间")
    update_by: Optional[int] = Field(default=None, alias="updateBy", description="修改人ID")


class DictItemBase(BaseSchema):
    """字典项表 基础模型"""

    dict_code: Optional[str] = Field(
        default=None, max_length=50, description="关联字典编码，与sys_dict表中的dict_code对应", alias="dictCode"
    )
    value: Optional[str] = Field(default=None, max_length=50, description="字典项值", alias="value")
    label: Optional[str] = Field(default=None, max_length=100, description="字典项标签", alias="label")
    tag_type: Optional[str] = Field(
        default=None, max_length=50, description="标签类型，用于前端样式展示（如success、warning等）", alias="tagType"
    )
    status: Optional[int] = Field(default=None, description="状态（1-正常，0-禁用）", alias="status")
    sort: Optional[int] = Field(default=None, description="排序", alias="sort")
    remark: Optional[str] = Field(default=None, max_length=255, description="备注", alias="remark")


class DictItemCreate(DictItemBase):
    """创建字典项表 模型"""

    pass


class DictItemUpdate(DictItemBase):
    """更新字典项表 模型"""

    dict_code: Optional[str] = Field(
        default=None, max_length=50, description="关联字典编码，与sys_dict表中的dict_code对应", alias="dictCode"
    )
    value: Optional[str] = Field(default=None, max_length=50, description="字典项值", alias="value")
    label: Optional[str] = Field(default=None, max_length=100, description="字典项标签", alias="label")
    tag_type: Optional[str] = Field(
        default=None, max_length=50, description="标签类型，用于前端样式展示（如success、warning等）", alias="tagType"
    )
    status: Optional[int] = Field(default=None, description="状态（1-正常，0-禁用）", alias="status")
    sort: Optional[int] = Field(default=None, description="排序", alias="sort")
    remark: Optional[str] = Field(default=None, max_length=255, description="备注", alias="remark")


class DictItemPublic(DictItemBase):
    """字典项表 公共模型，用于API响应"""

    id: int = Field(alias="id", description="主键")
    dict_code: Optional[str] = Field(
        default=None, alias="dictCode", max_length=50, description="关联字典编码，与sys_dict表中的dict_code对应"
    )
    value: Optional[str] = Field(default=None, alias="value", max_length=50, description="字典项值")
    label: Optional[str] = Field(default=None, alias="label", max_length=100, description="字典项标签")
    tag_type: Optional[str] = Field(
        default=None, alias="tagType", max_length=50, description="标签类型，用于前端样式展示（如success、warning等）"
    )
    status: Optional[int] = Field(default=None, alias="status", description="状态（1-正常，0-禁用）")
    sort: Optional[int] = Field(default=None, alias="sort", description="排序")
    remark: Optional[str] = Field(default=None, alias="remark", max_length=255, description="备注")
    create_time: Optional[datetime] = Field(default=None, alias="createTime", description="创建时间")
    create_by: Optional[int] = Field(default=None, alias="createBy", description="创建人ID")
    update_time: Optional[datetime] = Field(default=None, alias="updateTime", description="更新时间")
    update_by: Optional[int] = Field(default=None, alias="updateBy", description="修改人ID")


class DictPageVO(BaseSchema):
    """字典分页VO"""

    id: Optional[int] = Field(default=None, description="字典ID", alias="id")
    name: Optional[str] = Field(default=None, description="字典名称", alias="name")
    dict_code: Optional[str] = Field(default=None, description="字典编码", alias="dictCode")
    status: Optional[int] = Field(default=None, description="字典状态（1-启用，0-禁用）", alias="status")


class DictPageQuery(BaseSchema):
    """字典分页查询对象"""

    page_num: Optional[int] = Field(default=1, min=1, alias="pageNum", description="页码")
    page_size: Optional[int] = Field(default=10, min=10, max=1000, alias="pageSize", description="每页记录数")
    keywords: Optional[str] = Field(default=None, description="关键字(字典名称)", alias="keywords")


class DictForm(BaseSchema):
    """字典表单对象"""

    id: Optional[int] = Field(default=None, description="字典ID", alias="id")
    name: Optional[str] = Field(default=None, description="字典名称", alias="name")
    dict_code: Optional[str] = Field(default=None, description="字典编码", alias="dictCode")
    remark: Optional[str] = Field(default=None, description="备注", alias="remark")
    status: Optional[int] = Field(default=None, description="字典状态（1-启用，0-禁用）", alias="status")

    @field_validator("dict_code")
    @classmethod
    def dict_code_must_not_be_empty(cls, v):
        if not v or not v.strip():
            raise ValueError("字典编码不能为空")
        return v

    @field_validator("status")
    @classmethod
    def status_must_be_in_range(cls, v):
        if v is not None and (v < 0 or v > 1):
            raise ValueError("字典状态不正确")
        return v


class DictItemPageVO(BaseSchema):
    """字典项分页VO"""

    id: Optional[int] = Field(default=None, description="字典项ID", alias="id")
    dict_code: Optional[str] = Field(default=None, description="字典编码", alias="dictCode")
    label: Optional[str] = Field(default=None, description="字典标签", alias="label")
    value: Optional[str] = Field(default=None, description="字典值", alias="value")
    sort: Optional[int] = Field(default=None, description="排序", alias="sort")
    status: Optional[int] = Field(default=None, description="状态（1:启用，0:禁用）", alias="status")


class DictItemForm(BaseSchema):
    """字典项表单对象"""

    id: Optional[int] = Field(default=None, description="字典项ID", alias="id")
    dict_code: Optional[str] = Field(default=None, description="字典编码", alias="dictCode")
    value: Optional[str] = Field(default=None, description="字典项值", alias="value")
    label: Optional[str] = Field(default=None, description="字典项标签", alias="label")
    sort: Optional[int] = Field(default=None, description="排序", alias="sort")
    status: Optional[int] = Field(default=None, description="状态（0：禁用，1：启用）", alias="status")
    tag_type: Optional[str] = Field(default=None, description="字典类型（用于显示样式）", alias="tagType")
